package com.nf.health.app.utils.pay;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;

public class SignUtils {
	private static final String ALGORITHM = "RSA";
	private static final String SIGN_ALGORITHMS = "SHA1WithRSA";
	private static final String DEFAULT_CHARSET = "UTF-8";
	public static String sign(String content, String privateKey) {
		try {
			java.security.Signature signature = java.security.Signature
					.getInstance(SIGN_ALGORITHMS);
			PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(PayBase64.decode(privateKey));
			KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
			PrivateKey priKey = keyFactory.generatePrivate(privSpec);
			signature.initSign(priKey);
			signature.update(content.getBytes(DEFAULT_CHARSET));
			byte[] signed = signature.sign();
			return PayBase64.encode(signed);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}



	/*PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(PayBase64.decode(privateKey));
			KeyFactory keyf = KeyFactory.getInstance(ALGORITHM);
			PrivateKey priKey = keyf.generatePrivate(priPKCS8);*/


}
